from fastapi import APIRouter, BackgroundTasks
from pydantic import BaseModel

from paper_tool.generateAndValidateEfaData import (
    generate_data,
    generate_valigate_save_excel,
)

from paper_tool.scriptDev2.start import run_start


class ParaItem(BaseModel):
    num_sample: int  # 或者 int，根据你的需求选择
    num_variabel: int  # 或者 int
    desc: str


router = APIRouter(prefix="/data", tags=["data_analysis"])


@router.get("/producetest", summary="测试调用paper_tool包的接口")
async def produce_test():
    temp_data = generate_data(50, 5)
    # print(temp_data)
    return {"data": temp_data.tolist()}


@router.post(
    "/generate_valigate_save_excel_api",
    summary="数据产生，验证，并保存到excel",
)
async def generate_valigate_save_excel_api(
    param_data: ParaItem, background_tasks: BackgroundTasks
):
    # print(temp_data)
    # generate_valigate_save_excel(param_data.num_sample,param_data.num_variabel)
    background_tasks.add_task(
        generate_valigate_save_excel, param_data.num_sample, param_data.num_variabel
    )
    return {"msg": "数据生成与验证任务已经运行 in the background"}


@router.get(
    "/run_start_api",
    summary="生成数据的全流程，执行耗时比较长，勿频繁请求",
)
async def run_start_api(background_tasks: BackgroundTasks):

    background_tasks.add_task(run_start)
    # print(temp_data)
    return {"data": "任务已在后台运行, 请耐心等待."}

@router.get("/describe", tags=["data_analysis"], summary="描述性统计")
async def describe_api():
    return {"data": "ok"}


@router.get("/hypothesis_testing", tags=["data_analysis"], summary="假设检验")
async def hypothesis_testing_api():
    return {"data": "ok"}


@router.get("/efa", tags=["data_analysis"], summary="探索性因子分析")
async def efa_api():
    return {"data": "ok"}


@router.get("/cfa", tags=["data_analysis"], summary="验证性因子分析")
async def cfa_api():
    return {"data": "ok"}


@router.get("/sem", tags=["data_analysis"], summary="结构方程模型")
async def sem_api():
    return {"data": "ok"}


@router.get("/regulate_sem_api", tags=["data_analysis"], summary="调节模型")
async def regulate_sem_api():
    return {"data": "start-ok"}


@router.get("/lpa_api", tags=["data_analysis"], summary="潜在剖面分析")
async def lpa_api():
    return {"data": "start-ok"}


@router.get("/lca_api", tags=["data_analysis"], summary="潜在类别分析")
async def lca_api():
    return {"data": "start-ok"}
